var DAO = require('./DAO')
//针对用户数据表操作的模块对象

var shareDAO = {
    //分享圈
    shareContent: function (callback) {
        DAO('select upmenu.*,users.userId,users.userName,users.headPic from upmenu,users where upmenu.touserId = users.userId order by upTime desc;', null, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //已发布内容
    publish: function (user, callback) {
        DAO('select distinct upmenu.upmenuId,upmenu.upmenuTitle,upmenu.finishPic,upmenu.upTime,upmenu.story  from upmenu join users where upmenu.touserId in (select userId from users where phone = ?) order by upTime desc', [user.phone], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //分享用户的信息
    shareuser: function (upmenuId, callback) {
        DAO('select * from users where userId in (select touserId from upmenu where upmenuId = ?)', upmenuId, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //分享圈详情
    sharedetail: function (upmenuId, callback) {
        DAO('select step.stepId,upmenu.upmenuTitle,step.stepPic,step.stepMake from upmenu join step on upmenu.upmenuId= step.toupmenuId where toupmenuId =?;', upmenuId, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //已发布内容的详情
    publishdetail: function (upmenuId, callback) {
        DAO('select upmenu.*,step.stepId,upmenu.upmenuTitle,step.stepPic,step.stepMake from upmenu join step on upmenu.upmenuId= step.toupmenuId where toupmenuId =?;', [upmenuId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //上传菜谱
    // upmenu: function (upmenu, callback) {
    //     DAO('insert into upmenu(upmenuTitle,finishPic,story,upTime,touserId) VALUES(?,?,?,now(),?)', [upmenu.upmenuTitle, upmenu.finishPic, upmenu.story, upmenu.touserId], function (err, results) {
    //         if (err) {
    //             callback(err, null)
    //         } else {
    //             callback(null, results)
    //         }
    //     })
    // },
    // upmenustep:function(upmenu,callback){
    //     DAO('insert into step(stepPic,stepMake,toupmenuId) VALUES("?","?",?)', [upmenu.stepPic,upmenu.stepMake,upmenu.toupmenuId], function (err, results) {
    //         if (err) {
    //             callback(err, null)
    //         } else {
    //             callback(null, results)
    //         }
    //     })
    // },
    //上传菜谱
    upmenu: function (upmenu, callback) {
        DAO('insert into upmenu(upmenuTitle,finishPic,story,upTime,touserId) VALUES(?,?,?,now(),?)', [upmenu.upmenuTitle, upmenu.finishPic, upmenu.story, upmenu.touserId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    upmenustep: function (step, callback) {
        DAO('insert into step(stepPic,stepMake,toupmenuId) VALUES(?,?,(select max(upmenuId) from upmenu where touserId = ?)),(?,?,(select max(upmenuId) from upmenu where touserId = ?)),(?,?,(select max(upmenuId) from upmenu where touserId = ?)),(?,?,(select max(upmenuId) from upmenu where touserId = ?))', [step.stepPic1, step.stepMake1, step.touserId, step.stepPic2, step.stepMake2, step.touserId, step.stepPic3, step.stepMake3, step.touserId, step.stepPic4, step.stepMake4, step.touserId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },


    //删除已发布菜谱2个.
    personalConment: function (upmenuId, callback) {
        DAO('delete from step where toupmenuId = ?', [upmenuId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    deletepersonal: function (upmenuId, callback) {
        DAO('delete from upmenu where upmenuId = ?', [upmenuId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    //分享圈查新表
    selsharelike: function (user, callback) {
        DAO('select toupmenuId from sharelike where touserId =(select userId from users where phone = ? ) ', [user.phone], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    insertLike: function (arr, callback) {
        DAO('insert into sharelike(toupmenuId,touserId) values(?,(select userId from users where phone = ?))', [arr.upmenuId, arr.user.phone],
            function (err, results) {
                if (err) {
                    callback(err, null)
                } else {
                    callback(null, results)
                }
            })
    },

    //增加分享圈点赞数
    addUserlike: function (upmenuId, callback) {
        DAO('update upmenu set likeNum=likeNum+1 where upmenuId = ?', [upmenuId], function (err, results) {
            // console.log(results)
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    //    删除新表内容
    delLike: function (arr, callback) {
        DAO('delete from sharelike where toupmenuId = ? and touserId = (select userId from users where phone = ?)', [arr.upmenuId, arr.user.phone], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    //取消分享圈点赞数
    celUserlike: function (upmenuId, callback) {
        DAO('update upmenu set likeNum = likeNum-1 where upmenuId = ? ', [upmenuId], function (err, results) {
            console.log(results)
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    }
}
module.exports = shareDAO